﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace MVCApplication.Helpers
{
    public class PromptHelper
    {
        public static string JavaScriptUrl(string[] pk, string[] returnItems, System.Data.DataRow row)
        {
            // javascript:DK_js_return( new Array('1', '2' ) );

            System.Text.StringBuilder url = new System.Text.StringBuilder();
            url.Append("javascript:DK_js_return( new Array(");

            if (returnItems != null && returnItems.Length > 0)
            {
                string PkString = GetPrimaryKeyValueJavaScript(returnItems, row);
                url.Append(PkString);
            }
            else
            {
                string PkString = GetPrimaryKeyValueJavaScript(pk, row);
                if (PkString != null)
                {
                    url.Append(PkString);
                }

                if (row != null)
                {
                    bool find = false;

                    // append description item
                    if (row.Table.DataSet.ExtendedProperties["Deklarit.DescriptionAttribute"] != null)
                    {
                        foreach (DataColumn rowCol in row.Table.Columns)
                        {
                            if (row.Table.DataSet.ExtendedProperties["Deklarit.DescriptionAttribute"].ToString() == rowCol.ColumnName)
                            {
                                for (int i = 0; i < pk.Length; i++)
                                {
                                    url.Append(" ,");
                                    url.Append("'" + EscapeValue(row[rowCol.ColumnName].ToString()) + "'");
                                }
                                find = true;
                                break;
                            }
                        }
                    }

                    // append description item
                    if (find == false)
                    {
                        foreach (System.Data.DataColumn rowCol in row.Table.Columns)
                        {
                            if (rowCol.ExtendedProperties["Deklarit.IsDescription"] != null)
                            {
                                if (Convert.ToBoolean(rowCol.ExtendedProperties["Deklarit.IsDescription"].ToString()))
                                {
                                    for (int i = 0; i < pk.Length; i++)
                                    {
                                        url.Append(" ,");
                                        url.Append("'" + EscapeValue(row[rowCol.ColumnName].ToString()) + "' ");
                                    }
                                    find = true;
                                    break;
                                }
                            }
                        }
                    }

                    // if not append key value
                    if (find == false)
                    {
                        url.Append(" ,");
                        url.Append(PkString);
                        find = true;
                    }
                }
            }
            url.Append(" ) ) ; ");

            return url.ToString();
        }

        protected static string GetPrimaryKeyValueJavaScript(string[] pk, System.Data.DataRow row)
        {
            if (pk == null || row == null)
            {
                return null;
            }
            bool notFirst = false;

            System.Text.StringBuilder pkValue = new System.Text.StringBuilder();
            foreach (string pkKey in pk)
            {
                foreach (System.Data.DataColumn rowCol in row.Table.Columns)
                {
                    if (pkKey == rowCol.ColumnName)
                    {
                        if (notFirst)
                            pkValue.Append(" ,");

                        pkValue.Append("'" + EscapeValue(row[rowCol.ColumnName].ToString()) + "'");
                        notFirst = true;
                    }
                }
            }
            return pkValue.ToString();
        }

        private static string EscapeValue(string valueString)
        {
            StringBuilder outp = new StringBuilder();
            for (int i = 0; i < valueString.Length; i++)
            {
                char currentChar = valueString[i];
                switch (currentChar)
                {
                    case (char)8:
                        // Backspace  
                        outp.Append("\\010");
                        break;
                    case (char)9:
                        // Tab  
                        outp.Append("\\011");
                        break;
                    case (char)10:
                        // New line  
                        outp.Append("\\012");
                        break;
                    case (char)11:
                        // Vertical tab  
                        outp.Append("\\013");
                        break;
                    case (char)13:
                        // Carriage return
                        outp.Append("\\015");
                        break;
                    case (char)34:
                        // Double quote  
                        outp.Append("\\042");
                        break;
                    case (char)39:
                        // Apostrophe or single quote  
                        outp.Append("\\047");
                        break;
                    case (char)92:
                        // Backslash character
                        outp.Append("\\134");
                        break;
                    default:
                        {
                            outp.Append((char)currentChar);
                            break;
                        }
                }
            }
            return outp.ToString();
        }
    }
}
